package com.example.springmvcdemo;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping(value = "r1", method = RequestMethod.GET)
    public String r1() {
        return "hello";
    }

    @RequestMapping("r2")
    public String r2(String name) {
        return "你好" + name;
    }

    @RequestMapping("r3")
    public String r3(Integer age) {
        return "接收到参数" + age;
    }

    @RequestMapping("/r4")
    public String r4(String name, Integer age) {
        return "name:" + name + " age:" + age;
    }

    @RequestMapping("/r5")
    public String r5(UserInfo userInfo) {
        return userInfo.toString();
    }

    @RequestMapping("r6")
    public String r6(@RequestParam(value = "name", required = false) String username,
                     @RequestParam(value = "age", required = false) Integer userage) {
        return "username:" + username + " userage: " + userage;
    }

    @RequestMapping("/r7")
    public String r7(String[] arr) {
        Arrays.stream(arr).forEach(x-> System.out.println(x));

        return "参数接收成功" + Arrays.toString(arr);
    }

    @RequestMapping("/r8")
    public String r8(@RequestParam(value = "list", required = false) List<String> list) {
        if (list != null) {
            return list.size() + "";
        }

        return "list为null";
    }

    @RequestMapping("/r9")
    public String r9(@RequestBody UserInfo userInfo) {
        return userInfo.toString();
    }

    @RequestMapping(value = {"r10/{name}/{id}", "r10/{id}"})
    public String r10(@PathVariable(value = "name", required = false) String name, @PathVariable(value = "id") Integer id) {
        return "name:" + name + " id:" + id;
    }

    @RequestMapping("/r11")
    public String r11(@RequestParam(value = "file")@RequestPart MultipartFile file1) throws IOException {
        String fileName = file1.getOriginalFilename();
        file1.transferTo(new File("D:/tmp/" + fileName));
        return "文件名为" + fileName;
    }
}
